$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'eys/ematerialbuy/list',
        datatype: "json",
        colModel: [			
			{ label: 'guid', name: 'guid', index: 'guid', width: 50, key: true },
			{ label: '所属项目ID', name: 'pGuid', index: 'p_guid', width: 80 }, 			
			{ label: '所属点位ID', name: 'pPGuid', index: 'p_p_guid', width: 80 }, 			
			{ label: '关联材料信息库ID', name: 'mMGuid', index: 'm_m_guid', width: 80 }, 			
			{ label: '材料大类', name: 'mBBigtype', index: 'm_b_bigtype', width: 80 }, 			
			{ label: '材料中类', name: 'mBMidtype', index: 'm_b_midtype', width: 80 }, 			
			{ label: '材料名称', name: 'mBName', index: 'm_b_name', width: 80 }, 			
			{ label: '主要参数', name: 'mBParameter', index: 'm_b_parameter', width: 80 }, 			
			{ label: '单重（kg）', name: 'mBWeight', index: 'm_b_weight', width: 80 }, 			
			{ label: '单价（元）', name: 'mBPrice', index: 'm_b_price', width: 80 }, 			
			{ label: '购置单位', name: 'mBUnit', index: 'm_b_unit', width: 80 }, 			
			{ label: '数量', name: 'mBCount', index: 'm_b_count', width: 80 }, 			
			{ label: '合重（kg）', name: 'mBWeightall', index: 'm_b_weightall', width: 80 }, 			
			{ label: '合价（元）', name: 'mBPriceall', index: 'm_b_priceall', width: 80 }, 			
			{ label: '数据来源    系统和人工', name: 'mBSource', index: 'm_b_source', width: 80 }, 			
			{ label: '数据状态', name: 'mBDatastate', index: 'm_b_datastate', width: 80 }, 			
			{ label: '设备标签', name: 'dLabel', index: 'd_label', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		eMaterialBuy: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.eMaterialBuy = {};
		},
		update: function (event) {
			var guid = getSelectedRow();
			if(guid == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(guid)
		},
		saveOrUpdate: function (event) {
			var url = vm.eMaterialBuy.guid == null ? "eys/ematerialbuy/save" : "eys/ematerialbuy/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.eMaterialBuy),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var guids = getSelectedRows();
			if(guids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "eys/ematerialbuy/delete",
                    contentType: "application/json",
				    data: JSON.stringify(guids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(guid){
			$.get(baseURL + "eys/ematerialbuy/info/"+guid, function(r){
                vm.eMaterialBuy = r.eMaterialBuy;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});